<?php
/**
 * Created by PhpStorm.
 * User: YONBIN
 * Date: 2020/11/27
 * Time: 14:58
 * Email: 479703456@qq.com
 */

namespace app\domain\admin;

use app\model\AdminGroup;
use app\model\AdminGroupMenusRelation;
use app\model\AdminMenus;
use think\response\Json;

class AdminGroupDomain extends CommonDomain
{
    public function retrieve($page, $pageSize, $search): array
    {
        try {
            $search = is_array($search) ? $search : [];
            $model = app(AdminGroup::class)->search($search);
            $total = $model->count();
            $list = $model->with('menus')->page($page, $pageSize)->order('id ASC')->select();

            $list->filter(function ($data) {
                $menus = app(AdminMenus::class)->where(" `pid` <> 0 ")->column('id');
                $ownerIds = array_column($data->menus->toArray(), 'menu_id');
                $menuIds = array_intersect($menus, $ownerIds);
                sort($menuIds);
                $data['menu_ids'] = $menuIds;
            });

            return [
                'total' => $total,
                'list' => $list
            ];
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return [];
        }
    }

    public function create($title, $description, $menu_ids, $status): bool
    {
        try {
            $model = app(AdminGroup::class);
            $result = $model->save([
                'title' => $title,
                'description' => $description,
                'status' => $status,
            ]);

            $id = $model->getLastInsID();
            $data = [];
            foreach ($menu_ids as $val) {
                $data[] = [
                    'group_id' => $id,
                    'menu_id' => $val,
                ];

            }
            if ($data) {
                app(AdminGroupMenusRelation::class)->saveAll($data);
            }

            return (bool)$result;
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }

    public function update($id, $title, $description, $menu_ids, $status): bool
    {
        try {
            $info = app(AdminGroup::class)->with('menus')->find($id);
            if (empty($info)) {
                $this->errMsg = 'record.not_exists';
                return false;
            }

            $result = $info->save([
                'title' => $title,
                'description' => $description,
                'status' => in_array($status, ['enable', 'disable']) ? $status : 'enable',
            ]);

            $menus = is_array($info->toArray()['menus']) ? $info->toArray()['menus'] : [];
            $menusIds = array_column($menus, 'menu_id');
            $diffIds = array_diff($menu_ids, $menusIds);

            $delIds = array_diff($menusIds, $menu_ids);
            if ($delIds) {
                sort($delIds);
                app(AdminGroupMenusRelation::class)->where(['group_id' => $id])->whereIn('menu_id', $delIds)->delete();
            }

            $menusAllIds = app(AdminMenus::class)->column('id');

            $data = [];
            foreach ($diffIds as $val) {
                if (in_array($val, $menusAllIds)) {
                    $data[] = [
                        'group_id' => $id,
                        'menu_id' => $val,
                    ];
                }
            }
            if ($data) {
                app(AdminGroupMenusRelation::class)->saveAll($data);
            }

            return (bool)$result;
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }

    public function delete($id): bool
    {
        try {
            $info = app(AdminGroup::class)->with('menus')->find($id);
            if (empty($info)) {
                $this->errMsg = 'record.not_exists';
                return false;
            }

            $result = $info->together(['menus'])->delete();

            return (bool)$result;
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }
}